package com.zy.qudadid.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.services.core.PoiItem;
//import com.zhy.http.okhttp.OkHttpUtils;
//import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zy.app.CarAppConts;
import com.zy.lcpassenger.R;
import com.zy.qudadid.model.User;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.presenter.base.BasePresenterImp;
import com.zy.qudadid.ui.activity.base.ToolBarActivity;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.Bun;
import com.zy.qudadid.utils.StringUtil;
import com.zy.qudadid.utils.UserUtil;
import com.zy.util.JsonUtils;
import com.zy.util.Util;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by Administrator on 2016/7/30 0030.
 */
public class ChangyongdizhiActivity extends ToolBarActivity implements View.OnClickListener {

    TextView  home, com;
    ImageView jiashanchu, gongsishanchu;
    RelativeLayout startadds, endadds;
    PoiItem poiItemStart;
    PoiItem poiItemEnd;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_changyongdizhi;
    }

    @Override
    public void initListeners() {

    }

    @Override
    public BasePresenterImp createPresenter() {
        return null;
    }

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);
//        back = (ImageView) findViewById(R.id.back);
//        bianji = (TextView) findViewById(R.id.bianji);
        home = (TextView) findViewById(R.id.home);
        com = (TextView) findViewById(R.id.com);
        jiashanchu = (ImageView) findViewById(R.id.jiashanchu);
        gongsishanchu = (ImageView) findViewById(R.id.gongsishanchu);
        startadds = (RelativeLayout) findViewById(R.id.startadds);
        endadds = (RelativeLayout) findViewById(R.id.endadds);
        User u = new UserUtil(this).getUser();
        if (StringUtil.isEmpty(u.home_addr)) {
            home.setText("请选择地址");
        } else {
            home.setText(u.home_addr);
        }
        if (StringUtil.isEmpty(u.com_addr)) {
            com.setText("请选择地址");
        } else {
            com.setText(u.com_addr);
        }
//        bianji.setText("删除");
        jiashanchu.setVisibility(View.GONE);
        gongsishanchu.setVisibility(View.GONE);
//        bianji.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (jiashanchu.getVisibility() == View.GONE) {
//                    jiashanchu.setVisibility(View.VISIBLE);
//                    gongsishanchu.setVisibility(View.VISIBLE);
//                    bianji.setText("完成");
//                } else {
//                    jiashanchu.setVisibility(View.GONE);
//                    gongsishanchu.setVisibility(View.GONE);
//                    bianji.setText("删除");
//                }
//
//            }
//        });

        startadds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(IndexSelectaddsActivity.class, new Bun().putString("state", "out").putString("city", "中国").ok(), 1020);
            }
        });
        endadds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(IndexSelectaddsActivity.class, new Bun().putString("state", "out").putString("city", "中国").ok(), 1021);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1020 && resultCode == 2010) {
            poiItemStart = data.getParcelableExtra("poiitem");
            saveInfoH(poiItemStart.getTitle(), poiItemStart.getLatLonPoint().getLongitude() + "", poiItemStart.getLatLonPoint().getLatitude() + "");

        } else if (requestCode == 1021 && resultCode == 2010) {
            poiItemEnd = data.getParcelableExtra("poiitem");
            saveInfoC(poiItemEnd.getTitle(), poiItemEnd.getLatLonPoint().getLongitude() + "", poiItemEnd.getLatLonPoint().getLatitude() + "");
        }

    }

    public void saveInfoH(final String hadds, final String haddslng, final String haddslat) {
        final User user = new UserUtil(this).getUser();
        //修改用户信息
        try {
            HashMap<String, String> map = new HashMap<>();
            AESCrypt aesCrypt = new AESCrypt();
            map.put("user_id", new UserUtil(getBaseContext()).getUserId());
            map.put("type", "1");
            map.put("home_address", hadds);
            map.put("lng", haddslng);
            map.put("lat", haddslat);
            String json = JsonUtils.toJson(map);
            String d = Util.replaceBlank(aesCrypt.encrypt(json));
            OkHttpUtils.post()
                    .url(Const.BASE_URL + "api.php/User/often_home_add")
                    .addParams("args", Util.replaceBlank(aesCrypt.encrypt(json)))
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call,Exception e,int i) {
                            Toast.makeText(getBaseContext(), "用户信息修改失败", Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onResponse(String response,int i) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                if (jsonObject.getString("code").equals("200")) {
                                    Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                                    home.setText(hadds);
                                    user.home_addr = hadds;
                                    user.home_addr_lng = haddslng;
                                    user.home_addr_lat = haddslat;
                                    new UserUtil(getBaseContext()).removeUserInfo();
                                    new UserUtil(getBaseContext()).putUser(user);
                                } else {
                                    Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Log.i("GGGG","denglu加密==="+ Util.replaceBlank(aesCrypt.encrypt(json))+"     lainjie==="+CarAppConts.base_url+"login_with_reg&args="+Util.replaceBlank(aesCrypt.encrypt(json)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveInfoC(final String cadds, final String caddslng, final String caddslat) {
        final User user = new UserUtil(this).getUser();
        //修改用户信息
        try {
            HashMap<String, String> map = new HashMap<>();
            AESCrypt aesCrypt = new AESCrypt();
            map.put("user_id", new UserUtil(getBaseContext()).getUserId());
            map.put("type", "2");
            map.put("home_address", cadds);
            map.put("lng", caddslng);
            map.put("lat", caddslat);
            String json = JsonUtils.toJson(map);
            String d = Util.replaceBlank(aesCrypt.encrypt(json));
            OkHttpUtils.post()
                    .url(CarAppConts.base_url + "api.php/User/often_home_add")
                    .addParams("args", Util.replaceBlank(aesCrypt.encrypt(json)))
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int i) {
                            Toast.makeText(getBaseContext(), "用户信息修改失败", Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onResponse(String response, int i) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                if (jsonObject.getString("code").equals("200")) {
                                    Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                                    com.setText(cadds);
                                    user.com_addr = cadds;
                                    user.com_addr_lng = caddslng;
                                    user.com_addr_lat = caddslat;
                                    new UserUtil(getBaseContext()).removeUserInfo();
                                    new UserUtil(getBaseContext()).putUser(user);
                                } else {
                                    Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Log.i("GGGG", "denglu加密===" + Util.replaceBlank(aesCrypt.encrypt(json)) + "     lainjie===" + CarAppConts.base_url + "login_with_reg&args=" + Util.replaceBlank(aesCrypt.encrypt(json)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void startActivityForResult(Class clazz, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getBaseContext(), clazz);
        intent.putExtra("data", bundle);
        startActivityForResult(intent, requestCode);
    }

    @Override
    protected CharSequence provideTitle() {
        return "常用地址";
    }

    @Override
    public void onClick(View v) {

    }
}
